package zara
import org.codehaus.groovy.grails.plugins.springsecurity.Secured
@Secured(['ROLE_USER','ROLE_ADMIN'])
class TaskController {
    def authenticateService
    static allowedMethods = [save: "POST", update: "POST"]
    static defaultAction = "index"


//    def beforeInterceptor = {
//        if(params.websiteid){
//            def websiteInstance = Website.get(params.websiteid) 
//            def person = authenticateService.userDomain()
//
//            if(websiteInstance && websiteInstance.person.id != authenticateService.userDomain().id){
//                    flash.message = "您没有权限访问！！！！！"
//                    redirect(redirect(uri:"/autherror"))
//            }
//        }
//    }


    def index = {
        def websiteInstance = Website.get(params.websiteid) 
        if(websiteInstance && websiteInstance.person.id != authenticateService.userDomain().id){
                flash.message = "您没有权限访问！！！！！"
                redirect(redirect(uri:"/autherror"))
        }
        session.website = websiteInstance
        redirect(action: "list", params: params)
    }

    def list = {        
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.order="asc"
        params.sort="website"
        def person = authenticateService.userDomain()
        [taskInstanceList: Task.findAllByWebsiteAndPerson(session.website,person,params), taskInstanceTotal: Task.countByWebsiteAndPerson(session.website,person)]
    }

    def create = {
        def taskInstance = new Task()
        taskInstance.properties = params
        taskInstance.website = session.website
        return [taskInstance: taskInstance]
    }

    def save = {
        def taskInstance = new Task(params)
        taskInstance.person = authenticateService.userDomain()
        if (taskInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'task.label', default: 'Task'), taskInstance])}"
            redirect(action: "list")
        }else {
            render(view: "create", model: [taskInstance: taskInstance])
        }
    }

    def show = {
        def taskInstance = Task.get(params.id)
        if (!taskInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
            redirect(action: "list")
        }
        else {
            [taskInstance: taskInstance]
        }
    }

    def edit = {
        def taskInstance = Task.get(params.id)
        if (!taskInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [taskInstance: taskInstance]
        }
    }

    def update = {
        def taskInstance = Task.get(params.id)
        if (taskInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (taskInstance.version > version) {
                    
                    taskInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'task.label', default: 'Task')] as Object[], "Another user has updated this Task while you were editing")
                    render(view: "edit", model: [taskInstance: taskInstance])
                    return
                }
            }
            taskInstance.properties = params
            if (!taskInstance.hasErrors() && taskInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'task.label', default: 'Task'), taskInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [taskInstance: taskInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def taskInstance = Task.get(params.id)
        if (taskInstance) {
            if(taskInstance.person.id != authenticateService.userDomain().id){
                redirect(redirect(uri:"/autherror"))
            }
            try {
                taskInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'task.label', default: 'Task'), taskInstance])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'task.label', default: 'Task'), taskInstance])}"
                redirect(action: "list")
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
            redirect(action: "list")
        }
    }

    def close = {
        Task task = Task.get(params.id)
        if (task) {
            task.available = false
            task.save();
        }
        redirect(action: "list")
    }

    def open = {
        Task task = Task.get(params.id)
        if (task) {
            task.available = true
            task.save();
        }
        redirect(action: "list")
    }
}
